package huixue.cn.com.rxapp.ui;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.Toolbar;
import android.view.animation.DecelerateInterpolator;

import butterknife.BindView;
import butterknife.ButterKnife;
import huixue.cn.com.library.util.ImageLoaderUtil;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.BaseActivity;

import huixue.cn.com.rxapp.base.BasePresent;
import huixue.cn.com.rxapp.base.SimpleActivity;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class PictureActivity extends SimpleActivity {

    public static final String EXTRA_IMAGE_URL = "image_url";
    public static final String EXTRA_IMAGE_TITLE = "image_title";
    public static final String TRANSIT_PIC = "picture";

    @BindView(R.id.picture)
    PhotoView mImageView;

    @BindView(R.id.app_bar_layout)
    protected AppBarLayout mAppBar;
    @BindView(R.id.toolbar)
    protected Toolbar mToolbar;

    PhotoViewAttacher mPhotoViewAttacher;
    String mImageUrl, mImageTitle;

    private boolean mIsHidden = false;


    @Override
    public int getLayout() {
        return R.layout.activity_picture;
    }

    @Override
    public void initView() {
        setSupportActionBar(mToolbar);
        parseIntent();
        ViewCompat.setTransitionName(
                mImageView,
                TRANSIT_PIC);
        //setAppBarAlpha(0.7f);
        setTitle(mImageTitle);
        ImageLoaderUtil.loadGifImg(mImageView, mImageUrl);
        mPhotoViewAttacher = new PhotoViewAttacher(mImageView);
        mPhotoViewAttacher.setOnViewTapListener((view, v, v1) -> hideOrShowToolbar());
    }

    @Override
    public void addEvent() {

    }


    private void parseIntent() {
        mImageUrl = getIntent().getStringExtra(EXTRA_IMAGE_URL);
        mImageTitle = getIntent().getStringExtra(EXTRA_IMAGE_TITLE);
    }

    public static Intent newIntent(Context context, String url, String desc) {
        Intent intent = new Intent(context, PictureActivity.class);
        intent.putExtra(PictureActivity.EXTRA_IMAGE_URL, url);
        intent.putExtra(PictureActivity.EXTRA_IMAGE_TITLE, desc);
        return intent;
    }

    protected void hideOrShowToolbar() {
        mAppBar.animate()
                .translationY(mIsHidden ? 0 : -mAppBar.getHeight())
                .setInterpolator(new DecelerateInterpolator(2))
                .start();
        mIsHidden = !mIsHidden;
    }


}
